package interview.hash;

import java.util.*;

/**
 * 寻找合法三元组 https://codefun2000.com/p/P1083
 */
public class LegalTriplet {

    /**
     * 根据题意可知，即寻找顺序无关的三元组(x-1,x,x)或(x-1,x-1,x)，故只需要用一个hashmap记录每种元素出现的次数，然后枚举每种元素可能的三元组即可
     * 针对每个元素x，第—种情况的贡献:C(d[x],2) * d[x -1] 第二种情况的贡献:C(d[x-1],2) * d[x]
     */
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        HashMap<Long, Long> map = new HashMap<>();
        for (int i = 0;i < n;i++) {
            long num = sc.nextLong();
            map.put(num, map.getOrDefault(num, 0L) + 1);
        }
        long count = 0;
        for (Map.Entry<Long, Long> entry : map.entrySet()) {
            long x = entry.getKey(), d = entry.getValue();
            if (map.get(x-1) == null) continue;
            long dx_1 = map.get(x-1);
            count += (d*(d-1)/2) * dx_1 ;
            count += (dx_1*(dx_1-1)/2) * d ;
        }
        System.out.println(count);
    }


    /**
     * 超时。题目元素数量为20w个，不能用回溯
     */
    static long count = 0;
    static int[] nums;
    public static void main2(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        nums = new int[n];
        for (int i = 0;i < n;i++)
            nums[i] = sc.nextInt();
        backTracing(new ArrayList<>(), 0);
        System.out.println(count);
    }

    public static void backTracing(List<Integer> path, int begin) {
        if (path.size() == 3) {
            //这里不能对path进行排序，会打乱路径元素顺序导致无法回溯
            int max = Math.max(Math.max(path.get(0), path.get(1)), path.get(2));
            int min = Math.min(Math.min(path.get(0), path.get(1)), path.get(2));
            if (max - min == 1)
                count++;
            return;
        }
        for (int i = begin;i < nums.length;i++) {
            path.add(nums[i]);
            backTracing(path, i+1);
            path.remove(path.size()-1);
        }
    }
}
